import CanvasUtils from '@/utils/CanvasUtils';
import { Color, FontI, StyleI } from '@/utils/CanvasUtils/CanvasType';
import Paint from '@/paint/Paint';
import Base from '@/paint/elements/BaseElement';

export interface TextI extends FontI, StyleI {
	/** 文字颜色 */
	Color?: Color;
	/** 文字大小 */
	FontSize?: number;
	/** 字体 */
	FontFamily?: string;
	/** 文字基线 */
	BaseLine?: CanvasTextBaseline;
	/** 文字居中 */
	Align?: CanvasTextAlign;
}

export default class TextElement extends Base {

	public text: TextI;

	public save: FontI | null = null;

	public constructor(paint: Paint, text: TextI) {
		super(paint);
		this.text = text;
	}

	public Draw(canvas: CanvasUtils): void {
		const {
			Spot: { X, Y },
			Text, MaxNumber, BorderColor, LineWidth,
			Color, FontSize, FontFamily, BaseLine, Align
		} = this.text;
		const save = this.save = { Spot: { X, Y }, Text, MaxNumber };
		canvas.Save();
		if (FontSize) {
			canvas.FontSize = FontSize;
		}
		if (FontFamily) {
			canvas.FontFamily = FontFamily;
		}
		if (BaseLine) {
			canvas.TextBaseline = BaseLine;
		}
		if (Align) {
			canvas.TextAlign = Align;
		}
		if (Color) {
			canvas.FillStyle = Color;
			canvas.FillText(save);
		}
		if (BorderColor) {
			canvas.StrokeStyle = BorderColor;
			if (LineWidth) {
				canvas.LineWidth = LineWidth;
			}
			canvas.StrokeText(save);
		}
		canvas.Restore();
	}

	public OnRange(): boolean {
		return false;
	}
}
